package com.duomn.mybatis.plugins.tools;

import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.duomn.mybatis.plugins.model.ExecuteMode;
import com.duomn.mybatis.plugins.model.ParsedStatement;

public class ShareContext {
	/** 缓存statement */
	private static final String LOCAL_STATEMETN = "local_statement";
	/** 一个mapper中执行的深度，只有最外层的才进行拦截  */
	private static final String STACK_DEEPTH = "stack_deepth";
	/** 是否进行分表拦截 */
	private static final String SHARDING_INTERCEPT = "sharding_intercept";
	/** 解析的对象 */
	private static final String PARSED_STATEMET = "parsed_statemet";
	
	/** 执行的方式 */
	private static final String EXECUTE_MODE = "execute_mode";
	
	private static ThreadLocal<Map<String, Object>> LOCAL = new ThreadLocal<Map<String, Object>>() {
		protected Map<String, Object> initialValue() {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(STACK_DEEPTH, -1);
			map.put(LOCAL_STATEMETN, new HashMap<Integer, List<Statement>>(2));  
			return map;
		}
	};
	

	/***
	 * 设置当前的解析类
	 * @param parsedStatement
	 */
	public static void setParsedStatement(ParsedStatement parsedStatement) {
		LOCAL.get().put(PARSED_STATEMET, parsedStatement);
	}
	
	/**
	 * 获取当前的解析类
	 * @return
	 */
	public static ParsedStatement getParsedStatement() {
		return (ParsedStatement) LOCAL.get().get(PARSED_STATEMET);
	}
	
	public static void setExecuteMode(ExecuteMode mode) {
		LOCAL.get().put(EXECUTE_MODE, mode);
	}
	
	public static ExecuteMode getExecuteMode() {
		return (ExecuteMode) LOCAL.get().get(EXECUTE_MODE);
	}
	
	/***
	 * 是否进行分表拦截
	 * @return
	 */
	public static boolean isSharding() {
		Map<String, Object> map = LOCAL.get();
		if (map.containsKey(SHARDING_INTERCEPT)) {
			return (Boolean) map.get(SHARDING_INTERCEPT);
		}
		return true;
	}
	
	public static void setSharding(boolean sharding) {
		LOCAL.get().put(SHARDING_INTERCEPT, sharding);
	}
	
	/**
	 * 在当前执行的statmentList中放入statment
	 * @param statement
	 */
	public static void put(Statement statement) {
		Map<Integer, List<Statement>> stsMap = (Map<Integer, List<Statement>>) LOCAL.get().get(LOCAL_STATEMETN);
		Integer currDep = (Integer) LOCAL.get().get(STACK_DEEPTH);
		List<Statement> sts = stsMap.get(currDep);
		if (sts == null) {
			sts = new ArrayList<Statement>(5);
			stsMap.put(currDep, sts);
		}
		sts.add(statement);
	}
	
	/** 获取当前执行的sql */
	public static List<Statement> get() {
		Map<Integer, List<Statement>> stsMap = (Map<Integer, List<Statement>>) LOCAL.get().get(LOCAL_STATEMETN);
		Integer currDep = (Integer) LOCAL.get().get(STACK_DEEPTH);
		return stsMap.get(currDep);
	}
	
	/**
	 * statement中再创建的statment
	 */
	public static void increaeDep() {
		Integer currDep = (Integer) LOCAL.get().get(STACK_DEEPTH);
		LOCAL.get().put(STACK_DEEPTH, currDep + 1);
	}
	
	/**
	 * 当前statment执行完成
	 */
	public static void decreaeDep() {
		Integer currDep = (Integer) LOCAL.get().get(STACK_DEEPTH);
		LOCAL.get().put(STACK_DEEPTH, currDep - 1);
		Map<String, Object> map = (Map<String, Object>) LOCAL.get().get(LOCAL_STATEMETN);
		map.remove(currDep);
	}
	
	/**
	 * 执行完成后clear掉
	 */
	public static void clear() {
		LOCAL.remove();
	}
	
	/**
	 * 判断是否处理完成
	 * @return
	 */
	public static boolean isBottom() {
		return 0 == (Integer) LOCAL.get().get(STACK_DEEPTH);
	}
	
	public static boolean isEmpty() {
		return -1 == (Integer) LOCAL.get().get(STACK_DEEPTH);
	}
	
}
